package top.eggcode.system.modules.monitor.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Publisher {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * @return void
     * @Title 生产者将消息发送到Direct交换机
     * @Description 发送的时候携带RoutingKey
     * @Author long.yuan
     * @Date 2020/2/29 13:29
     * @Param [i]
     **/
    public void sendMessage(String messageBody) {
        System.out.println("发送消息 : " + messageBody);
        MessageConverter messageConverter = new SimpleMessageConverter();
        MessageProperties properties = new MessageProperties();

        // 消息持久化
        properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message message = messageConverter.toMessage(messageBody, properties);


        rabbitTemplate.convertAndSend("common", "monitor-metric-upload", message);
    }
}
